<?php

namespace backend\models;

use Yii;

class Department extends \common\models\Department
{
    public function rules()
    {
        return array_merge([
          [['BMDM', 'FZRGH'], 'match', 'pattern' => '/^[a-zA-Z0-9]+$/','message' => '该项填写内容包含非法字符'],  // 代号类字段
          [['CLSJ', 'CXSJ'], 'date', 'format' => 'php:Y-m-d', 'message' => '错误的日期格式，请重新修改'],
        ], parent::rules());
    }

    //提供关联数组 BMDM => BMMC ，可用于下拉框
    public static function getDepartID($school_id=null){
        $query=self::find()->select(['BMMC'])->orderBy('ID')->indexBy('ID');
        if($school_id){
            $query->andWhere(['XXID'=>$school_id]);
        }
        return $query->column();
    }

    /////////////// 新建初始记录 ///////////////
    public function createNewRecord($id)
    {
        $departmentModel = new Department();
        $departmentModel->XXID = $id;
        $departmentModel->BMMC = '待补充';
        $departmentModel->BMDM = '待补充';
        $departmentModel->FZRXM = '待补充';
        $departmentModel->CLSJ = date('Y-m-d', time());
        $departmentModel->ZT = '正常';
        $departmentModel->save(false);
        return $departmentModel;
    }
}